package com.cn.onetrip.activity;

import java.util.List;

import com.cn.onetrip.adapter.VideosGridAdapter;
import com.cn.onetrip.adapter.VideosItemAdapter;
import com.cn.onetrip.objects.VideosMediaObj;
import com.cn.onetrip.untility.SysApplication;
import com.cn.onetrip.activity.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;

public class VideosGridActivity extends Activity implements OnItemClickListener {

	private GridView gridView;
	private VideosGridAdapter adapter;
	private Button quit;
	private List<VideosMediaObj> videosMediaList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		if (SysApplication.videoList.size() == 0
				&& SysApplication.visitsListObj.size() == 0) {
			finish();
			android.os.Process.killProcess(android.os.Process.myPid());
		}
		setContentView(R.layout.activity_videos_grid);
		gridView = (GridView) findViewById(R.id.grid);
		adapter = new VideosGridAdapter(getApplicationContext());
		loadDate();
		gridView.setAdapter(adapter);
		gridView.setOnItemClickListener(this);
		quit = (Button) findViewById(R.id.quit);
		quit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
				overridePendingTransition(R.anim.push_left_translate,
						R.anim.pull_right_translate);
			}
		});
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		Intent intent = new Intent();
		Bundle bundle = new Bundle();
		intent.setClass(getApplicationContext(), ScenicVideoPlayActivity.class);
		bundle.putString("videoUrl", videosMediaList.get(arg2).mediaUrl);
		intent.putExtras(bundle);
		startActivity(intent);
		overridePendingTransition(R.anim.push_right_translate,
				R.anim.pull_left_translate);
	}

	public void loadDate() {
		adapter.clean();
		videosMediaList = SysApplication.videoList;
		// if (videosMediaList.size() == 1) {
		// return;
		// } else if (videosMediaList.size() == 2) {
		//
		// } else {
		for (int i = 0; i < videosMediaList.size(); i++) {
			VideosMediaObj videoObj = videosMediaList.get(i);
			adapter.addVideos(videoObj.mediaUrl, videoObj.cover,
					videoObj.duration, videoObj.name);
		}

		// }
	}

	@Override
	protected void onPause() {
		// adapter.clean();
		// adapter.threadQuit();
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		adapter.clean();
		adapter.threadQuit();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			finish();
			overridePendingTransition(R.anim.push_left_translate,
					R.anim.pull_right_translate);
		}
		return super.onKeyDown(keyCode, event);
	}
}
